<?php
/**
 * 内容管理
 *
 * @author kaikaic
 */

namespace app\psy\controller;

use app\admin\service\TagsService;
use app\psy\service\ArticleService;
use controller\BasicAdmin;
use service\ConfigService;
use service\DataService;

use think\Db;

class CounClass extends BasicAdmin {

    protected $table = 'PsyClass';

    protected $categoryKey = "doc";

    public function index() {
        $this->title = "课程管理";
        $title = $this->request->get("title");
        $categoryID = intval($this->request->param("cate_id"));

        $db = Db::name($this->table);
        if ($title) {
            $db->whereLike("title", "%{$title}%");
        }
        if ($categoryID) {
            $db->where(['category_id'=>$categoryID]);
        }

        return parent::listRecord($db);
    }

    /**
     * 列表数据处理
     */
    protected function _index_data_filter(&$data) {
        $size = count($data);

        // 文章分类
        $category = TagsService::getInstance()->getTagsArrayByAbbr("tags_factions");
        $cfgStatus = ConfigService::cfgArticleStatus();
        for ($i=0; $i<$size; $i++) {
            $data[$i]['status_txt'] = "";
            if (isset($cfgStatus[$data[$i]['status']])) {
                $data[$i]['status_txt'] = $cfgStatus[$data[$i]['status']];
            }

            $data[$i]['category_txt'] = "";
            if (isset($category[$data[$i]['category_id']])) {
                $data[$i]['category_txt'] = $category[$data[$i]['category_id']];
            }
        }

        $this->assign('cfg_status', $cfgStatus);
        $this->assign("category_list", $category);
        $this->assign("data_list", $data);
    }

    /**
     * 新建-文章
     */
    public function add() {
        $category =TagsService::getInstance()->getTagsArrayByAbbr("class_type");

        if ($this->request->isGet()) {
            return view('form', ['title' => '新建文章', 'image'=>ConfigService::defaultImage(), 'category_list'=>$category]);
        }

        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (($ids = $this->doInsertAction($data)) && !empty($ids)) {
                $this->showSuccess('文章添加成功！', '/psy/article/index');
            }

            $this->error('文章添加失败，请稍候再试！');
        }
    }

    /**
     * 编辑-文章
     */
    public function edit() {
        $category =TagsService::getInstance()->getTagsArrayByAbbr("class_type");
        $id = $this->request->get('id', '');
        if (empty($id)) {
            $this->error('参数错误，请稍候再试！');
        }

        if ($this->request->isGet()) {
            $article = ArticleService::getInstance()->find($id,'id',1);
            return view('form', ['title'=>'编辑文章', 'image'=>ConfigService::defaultImage(), 'category_list'=>$category, 'vo'=>$article]);
        }

        $data = $this->request->post();
        $ids = $this->doInsertAction($data);
        if (!empty($ids)) {
            $post = ['id' => $id, 'article_id' => $ids, 'create_by' => session('user.id')];
            if (false !== DataService::save('articles', $post, 'id')) {
                $this->showSuccess('课程更新成功!', '/psy/coun_class/index');
            }
        }

        $this->error('课程更新失败，请稍候再试！');
    }

    /**
     * 删除-文章
     */
    public function del() {
        $id = intval($this->request->post('id', ''));
        if (empty($id)) {
            $this->error('参数错误，请重新操作删除!');
        }

        $article = ArticleService::getInstance()->find($id);
        if (empty($article)) {
            $this->error('记录不存在，请重新操作!');
        }

        $result = Db::name($this->table)->where('id', $id)->delete();
        if ($result) {
            $this->success('文章删除成功!', '');
        }else {
            $this->error('文章删除失败，请重新再试!');
        }
    }

    protected function doInsertAction($data, $ids = []) {
        if (empty($data['id'])) {
            $data['status'] = 1;
            $data['create_time'] = time();
            return Db::name($this->table)->insertGetId($data);
        } else {
            $id = intval($data['id']);
            return Db::name($this->table)->where('id', $id)->update($data);
        }
    }
    public function changeTabelVal() {
        $id = intval($this->request->param('id', ''));
        if (empty($id)) {
            $this->error('参数错误，请重新操作删除!');
        }
        $value = $this->request->post('value',1);
        $field = $this->request->post('field','');
        Db::name($this->table)->where('id', $id)->update(array($field=>$value));
        $this->success('操作成功!', '');
    }

}

